package org.bp.spider.util;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class HttpClientUtils {
	public static String doPostParam(String url, Map<String, Object> param) {
		// 创建http POST请求
		HttpPost httpPost = new HttpPost("http://www.oschina.net/search");

		// 伪装成浏览器
		httpPost.setHeader("User-Agent",
				"Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36");

		// 设置2个post参数，一个是scope、一个是q
		List<NameValuePair> parameters = new ArrayList<NameValuePair>(0);
		parameters.add(new BasicNameValuePair("scope", "project"));
		parameters.add(new BasicNameValuePair("q", "java"));
		parameters.add(new BasicNameValuePair("fromerr", "7nXH76r7"));
		CloseableHttpResponse response = null;
		try {
			// 构造一个form表单式的实体
			UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters);
			// 将请求实体设置到httpPost对象中
			httpPost.setEntity(formEntity);

			// 创建Httpclient对象
			CloseableHttpClient httpclient = HttpClients.createDefault();

			// 执行请求
			response = httpclient.execute(httpPost);
			// 判断返回状态是否为200
			if (response.getStatusLine().getStatusCode() == 200) {
				String content = EntityUtils.toString(response.getEntity(), "UTF-8");
				return content;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			StreamUtils.close(response);
		}
		return null;
	}

	public static String doGetParam(String url, Map<String, Object> param) {
		// 定义请求的参数
		try {
			URI uri = new URIBuilder(url).setParameter("categoryId", "33").setParameter("page", "1")
					.setParameter("rows", "1").build();
			return doGet(uri.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String doGet(String url) {
		// 创建Httpclient对象
		CloseableHttpClient httpclient = HttpClients.createDefault();
		// 创建http GET请求
		HttpGet httpGet = new HttpGet(url);
		CloseableHttpResponse response = null;
		try {
			// 执行请求
			response = httpclient.execute(httpGet);
			// 判断返回状态是否为200
			if (response.getStatusLine().getStatusCode() == 200) {
				String content = EntityUtils.toString(response.getEntity(), "UTF-8");
				return content;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			StreamUtils.close(response);
		}
		return null;
	}

	public static String doPost(String url) {
		// 创建Httpclient对象
		CloseableHttpClient httpclient = HttpClients.createDefault();

		// 创建http POST请求
		HttpPost httpPost = new HttpPost("http://www.oschina.net/");
		// 伪装成浏览器
		httpPost.setHeader("User-Agent",
				"Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36");

		CloseableHttpResponse response = null;
		try {
			// 执行请求
			response = httpclient.execute(httpPost);
			// 判断返回状态是否为200
			if (response.getStatusLine().getStatusCode() == 200) {
				String content = EntityUtils.toString(response.getEntity(), "UTF-8");
				return content;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			StreamUtils.close(response);
		}
		return null;
	}
}
